﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmRecruitment : Form
    {

        RecruitmentInfo _RecruitmentInfo;
        DialogResult dlgResult;
        RecruitmentClient wcfRecruitment;
        RoleRightInfo listRole;
        bool isChange = false;
        public frmRecruitment(int RecruitmentID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            UpdateComponent();

            LoadLanguage();
            if (RecruitmentID > 0)
            {
                _RecruitmentInfo = wcfRecruitment.Recruitment_GetInfo(RecruitmentID);
                Info2Form();

            }
            _RecruitmentInfo.ID = RecruitmentID;
            listRole = _listRole;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole.Update)
                btnSave.Enabled = false;
            isChange = false;
        }
        
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        
        #region Form's Event

        private void UpdateComponent()
        {
            wcfRecruitment = Service.Recruitment();
            _RecruitmentInfo = new RecruitmentInfo();
            mtxFromDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        
        private void Info2Form()
        {
            txtNo.Text = _RecruitmentInfo.Name;
            mtxFromDate.Text =_RecruitmentInfo.FromDate == DateTime.MinValue?"": Convert.ToDateTime(_RecruitmentInfo.FromDate).ToString("dd/MM/yyyy");
            mtxToDate.Text = _RecruitmentInfo.ToDate == DateTime.MinValue ? "" : Convert.ToDateTime(_RecruitmentInfo.ToDate).ToString("dd/MM/yyyy");
            txtDescription.Text = _RecruitmentInfo.Description;
            isChange = false;
        }
        
        private void Form2Info()
        {
            _RecruitmentInfo.Name = txtNo.Text.Trim();
            _RecruitmentInfo.FromDate = UISetting.GetDate(mtxFromDate.Text);
            _RecruitmentInfo.ToDate = UISetting.GetDate(mtxToDate.Text);
            _RecruitmentInfo.Description = txtDescription.Text;
        }
        
        private bool CheckValid()
        {
            if (txtNo.Text.Trim().Length == 0)
            {
                txtNo.Focus();
                MessageBox.Show(UpdateUI.GetString("tenkhongduocdetrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (UISetting.GetDate(mtxFromDate.Text) == DateTime.MinValue)
            {
                mtxFromDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (UISetting.GetDate(mtxFromDate.Text) != DateTime.MinValue)
            {
                if (DateTime.Compare(UISetting.GetDate(mtxFromDate.Text),UISetting.GetDate(mtxToDate.Text))>0)
                {
                    mtxToDate.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngayketthucphaimuonhonngaybatdau"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            else
            {
                mtxToDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValid())
            {
                Form2Info();
                if (_RecruitmentInfo.ID > 0)//update
                {
                    if (!listRole.Update)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (wcfRecruitment.Recruitment_Exist(_RecruitmentInfo.ID, txtNo.Text.Trim()))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("datontai"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        return;
                    }
                    if (wcfRecruitment.Recruitment_Update(_RecruitmentInfo))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
                else
                {
                    if (!listRole.Create)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (wcfRecruitment.Recruitment_Exist(_RecruitmentInfo.ID, txtNo.Text.Trim()))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("datontai"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        return;
                    }
                    _RecruitmentInfo.ID = wcfRecruitment.Recruitment_Insert(_RecruitmentInfo);
                    if (_RecruitmentInfo.ID > 0)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            } 
            _RecruitmentInfo.ID = 0;
            txtNo.Text = "";
            mtxFromDate.Text = "";
            mtxToDate.Text = "";
            txtDescription.Text = "";
        }

        private void frmRecruitment_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            wcfRecruitment.Close();
            this.DialogResult = dlgResult;
        }

        private void frmRecruitment_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        #endregion

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

   

       

    }
}
